package day0721;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;

public class test {
    public static void main(String[] args) {
        JPanel p = new JPanel();
        p.setLayout(null);
        JLabel l = new JLabel(new ImageIcon("images/背景.jpg"));
        l.setLocation(0, 0);
        l.setSize(480, 800);
        p.add(l);
        ArrayList<JButton> btns = creatBtn();
        Collections.shuffle(btns);
        putBtns(btns, p, 20, 40, 7, 7, 0);
        putBtns(btns, p, 50, 80, 6, 6, 49);
        putBtns(btns, p, 15, 100, 5, 7, 85);
        putBtns(btns, p, 45, 140, 4, 6, 120);
        cheakCover(btns);
        ArrayList<JButton> through = new ArrayList<>();
        addClick(btns, through);
        JFrame f=new JFrame("羊了个羊");
        f.setSize(480,800);
        f.add(p);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    private static void addClick(ArrayList<JButton> btns, ArrayList<JButton> bottom) {
        for (int i = 0; i < btns.size(); i++) {
            JButton btn = btns.get(i);
            btn.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    btns.remove(btn);
                    //bottom.add(btn);
                    //btn.setLocation((bottom.size() - 1) * 60 + 27, 640);
                    int index=-1;
                    String same="no";
                    for (int j = 0; j < bottom.size(); j++) {
                        String bottomBtnName=bottom.get(j).getName();
                        String btnName=btn.getName();
                        if (bottomBtnName.equals(btnName)){
                            index=j;
                            same="yes";
                            break;
                        }
                    }
                    if(bottom.size()>=7){
                        //System.out.println("游戏结束");
                        JOptionPane.showMessageDialog(null,"游戏结束");
                        return;
                    }
                    if(same.equals("yes")){
                        bottom.add(index, btn);
                    }else{
                        bottom.add(btn);
                    }
                    for (int j = 0; j < bottom.size(); j++) {
                        System.out.println(bottom.get(j).getName());
                    }
                    for (int j = 0; j < bottom.size(); j++) {
                        bottom.get(j).setLocation(j* 60 + 27, 640);
                    }
                }
            });
        }
    }



    private static void cheakCover(ArrayList<JButton> btns) {
        for (int i = 0; i < btns.size() - 24; i++) {
            JButton btn = btns.get(i);
            int x = btn.getX() - 59;
            int x1 = btn.getX() + 59;
            int y1 = btn.getY() + 66;
            int y = btn.getY() - 66;
            for (int j = i + 1; j < btns.size(); j++) {
                JButton top = btns.get(j);
                int x2 = top.getX();
                int y2 = top.getY();
                if (x < x2 & x2 < x1 & y < y2 & y2 < y1) {
                    btn.setEnabled(false);
                }
            }
        }
    }



    private static void putBtns(ArrayList<JButton> btns, JPanel p, int x, int y, int row, int col, int start) {
        for (int i = 0; i < col * row; i++) {
            JButton btn = btns.get(start + i);
            btn.setSize(59, 66);
            btn.setLocation(i % col * 59 + x, i / col * 66 + y);
            p.add(btn, 0);
        }
    }

    private static ArrayList<JButton> creatBtn() {
        String[] a = new String[]{"兔子", "刷子", "剪刀", "南瓜", "奶瓶", "帽子", "干草", "手套", "月饼", "树", "树桩", "栗子",
                "毛线", "水桶", "火堆", "白菜", "耙子", "苹果", "草", "萝卜", "蜜蜂", "酒坛", "铃铛", "面具", "饭团"};
        ArrayList<JButton> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 25; j++) {
                JButton btn = new JButton(new ImageIcon("images/" + a[j] + ".png"));
                btn.setDisabledIcon(new ImageIcon("images/" + a[j] + "2.png"));
                btn.setName(a[j]);
                list.add(btn);
            }
        }
        return list;
    }
}
